#!/usr/bin/python
# -*- coding: utf-8 -*-
#file_op.py
#version 0.1  edited by lingyue.wkl 2011-10-1 11:52:11

#this script is userd to operate the file



import os,tarfile,fnmatch,glob

def make_tar(in_dir,out_dir,compression="gz"):
  if compression:
    dest_ext = "." + compression
  else:
    dest_ext = ""

  arc_name = os.path.basename(in_dir)
  dest_name = "%s.tar%s" %(arc_name,dest_ext)
  dest_path = os.path.join(out_dir,dest_name)

  if compression:
    dest_cmp = ":" + compression
  else:
    dest_cmp = ""
  
  out = tarfile.TarFile.open(dest_path,'w'+dest_cmp)
  out.add(in_dir,arc_name)
  out.close()
  return dest_path

def get_all_files(root,patterns="*",single_level=False,yield_folders=False):
  patterns = patterns.split(";")
  for path,subdirs,files in  os.walk(root):
    if yield_folders:
      files.extend(subdirs)
    files.sort()
    for name in files:
      for pattern in patterns:
        if fnmatch.fnmatch(name,pattern):
          yield os.path.join(path,name)
          break
    if single_level:
      break

def swap_extensions(in_dir, before, after):
  if before[:1] != ".":
    before = "." + before
  thelen = -len(before)
  if after[:1] != ".":
    after = "." + after
  for path,subdirs,files in os.walk(in_dir):
    for oldfile in files:
      if oldfile[thelen:] == before:
        oldfile = os.path.join(path,oldfile)
        newfile = oldfile[:thelen] + after
        os.rename(oldfile,newfile)

def search_file(filename,search_path,path_sep=os.pathsep):
  for path in search_path.split(path_sep):
    candidate = os.path.join(path,filename)
    if os.path.isfile(candidate):
      return os.path.abspath(candidate)
  return None  

def search_files(pattern,search_path, path_sep=os.pathsep):
  for path in search_path.split(path_sep):
    for match in glob.glob(os.path.join(path,pattern)):
      yield match

def count_files_in_dir(dir_path):
  files = os.listdir(dir_path)
  count = 0
  for file in files:
    myfile = dir_path + os.sep + file
    if os.path.isfile(myfile):
      count = count + 1
  return count    

def getDirList(space,l,dir_path):
  #print "begin"
  files = os.listdir(dir_path)
  #print files
  fileNum = count_files_in_dir(dir_path)
  tmpNum = 0
  for file in files:
    if ".svn" in file:
      continue
    myfile = dir_path + os.sep + file
    size = os.path.getsize(myfile)
    if os.path.isfile(myfile):
      tmpNum = tmpNum + 1
      if( tmpNum != fileNum):
        l.append(space+"|-" + file + "\n")
      else:
        l.append(space+"|_" + file + "\n")
    if os.path.isdir(myfile):
      l.append(space+ "|-[" + file + "]\n")
      space = space + "| "
      getDirList(space,l,myfile)
      space = space[:-4]
  #print "end"    
        

if __name__=="__main__":
    l = []
    getDirList("",l,"../")
    #print l
    for line in l:
      print line
    #make_tar("./test","./")
    #l = list(get_all_files("./"))
    #print l
    #swap_extensions("./test",".ab","cd")
    #print search_file("dir_op.py","./")
    #print list(search_files("*.py","./"))
